package com.photoeditor.demo.util.update.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.ui.dialog.BaseDialog;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.util.update.UpdateUtil;

/**
 * 作者：Administrator on 2016/8/3 20:34
 * 作用及描述：
 */
public class UpdateDialog extends BaseDialog {
    TextView tvLater, tvNow;
    View divider;
    UpdateUtil.UpdateInfo mUpdateInfo;

    public UpdateDialog(final Context context, UpdateUtil.UpdateInfo updateInfo) {
        super(context);
        if (updateInfo == null) {
            return;
        }
        mUpdateInfo = updateInfo;
        boolean bForcibly = updateInfo.getForcibly();
        if(bForcibly) {
            setCancelable(bForcibly);
            setCanceledOnTouchOutside(!bForcibly);
            if(tvLater != null) {
                tvLater.setVisibility(View.GONE);
                if(divider != null)
                    divider.setVisibility(View.GONE);
            }

            DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                        if (context instanceof Activity) {
                            // 强制升级弹窗按home键后，应用需要退到后台
                            ((Activity)context).moveTaskToBack(true);
                        }
                        return true;
                    } else {
                        return false;
                    }
                }
            };
            setOnKeyListener(keylistener);
        }
        setPositiveText(updateInfo.getStringInfo(UpdateUtil.UpdateInfo.TAG_CONFIRM, ""));
        setNegativeText(updateInfo.getStringInfo(UpdateUtil.UpdateInfo.TAG_CANCEL, ""));
    }

    public void setPositiveText(String confirm) {
        if (tvNow != null && !TextUtils.isEmpty(confirm)) {
            tvNow.setText(confirm);
        }
    }

    public void setNegativeText(String cancel) {
        if (tvLater != null && !TextUtils.isEmpty(cancel)) {
            tvLater.setText(cancel);
        }
    }

    @Override
    public void show() {
        super.show();
        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_APP_UPDATE_DIALOG,
                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ACTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_APP_UPDATE_DIALOG_VALUE_DISPLAY);
    }

    @Override
    public void initDefaultView(Context context) {
        super.initDefaultView(context);
        setContentView(R.layout.layout_update_avaliable);
        mRootView = findViewById(R.id.update_avalible_root);
        tvNow = (TextView) findViewById(R.id.update_now);
        tvLater = (TextView) findViewById(R.id.update_later);
        divider = findViewById(R.id.divider);
        mContent = (TextView) findViewById(R.id.message);
        mTitle = (TextView) findViewById(R.id.title);
        tvNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdateUtil.onUpdateClicked(mUpdateInfo, getContext());
                StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_APP_UPDATE_DIALOG,
                        StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ACTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_APP_UPDATE_DIALOG_DOWNLOAD);
                dismiss();
            }
        });
        tvLater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_APP_UPDATE_DIALOG,
                        StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ACTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_APP_UPDATE_DIALOG_CANCEL);
            }
        });
    }
}
